﻿using FineUI;
using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class ChangeZLAssign : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltySvrActList"; }
        }

        protected int QltyId
        {
            get
            {
                return GetRequestValue<int>("QltyId", 0);
            }
        }
        private AgentQltyInfo _objQlty = null;
        protected AgentQltyInfo objQlty
        {
            get
            {
                if (_objQlty == null)
                {
                    _objQlty = AgentQltyController.GetByQltyId(QltyId);
                }
                return _objQlty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            //权限代码
            this.ZLAssignUserId.Enabled=this.ZLRank.Enabled=this.ZLIsAfterSend.Enabled = this.User.UserId == 8 || this.User.UserId == 86 || User.IsSuperAdmin;
            this.ZLSituationReportUpload.Enabled = this.User.UserId == 67 || this.User.UserId == 144 || User.IsSuperAdmin;
            this.btnSaveClose.Visible = this.User.UserId == 67 || this.User.UserId == 144 || this.User.UserId == 8 || this.User.UserId == 86 || User.IsSuperAdmin;

            //数据绑定
            this.ZLAssignUserId.DataSource = OUserController.GetUserByDepartment("'质量管理部','技术部','生产管理部','生产部'");
            this.ZLAssignUserId.DataBind();
            this.ZLAssignUserId.Items.Add(new FineUI.ListItem() { Value = "0", Text = "无" });

            this.ZLAssignUserId.SelectedValue = objQlty.ZLAssignUserId.ToString();
            this.ZLRank.SelectedValue = objQlty.ZLRank;
            this.ZLIsAfterSend.Checked = objQlty.ZLIsAfterSend;


            //this.ZLSituationReportDownLoad.Hidden = ! string.IsNullOrEmpty(objQlty.ZLSituationReport);
            this.ZLSituationReportDownLoad.NavigateUrl = objQlty.ZLSituationReport;

        }

        protected void ZLSituationReportUpload_FileSelected(object sender, EventArgs e)
        {
            if (ZLSituationReportUpload.HasFile)
            {
                string fileName = ZLSituationReportUpload.ShortFileName;

                if (!ValidateFileType(fileName))
                {
                    Alert.Show("无效的文件类型！");
                    return;
                }


                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;

                string path = GetUpLoadFilePath(User.Department);
                ZLSituationReportUpload.SaveAs(Server.MapPath(path + fileName));

                // fileOther.EmptyText = path + fileName;
                // fileOther.ShowEmptyLabel = false;
                ZLSituationReportDownLoad.Hidden = false;
                ZLSituationReportDownLoad.NavigateUrl = path + fileName;
                ZLSituationReportDownLoad.Text = fileName;
                //  fileOther.Label = path + fileName;

                // 清空文件上传组件
                ZLSituationReportUpload.Reset();
            }
        }
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
           // AgentQltyInfo obj = obj;
            objQlty.QltyId = QltyId;
            objQlty.ZLAssignUserId = Convert.ToInt32(this.ZLAssignUserId.SelectedValue);
            objQlty.ZLAssignRealName = this.ZLAssignUserId.SelectedText;
            objQlty.ZLSituationReport = this.ZLSituationReportDownLoad.NavigateUrl;
            objQlty.ZLRank = this.ZLRank.SelectedText;
            objQlty.ZLIsAfterSend = this.ZLIsAfterSend.Checked;

            if (this.User.UserId == 8 || this.User.UserId == 86 || User.IsSuperAdmin)
            {
                objQlty.ZLAssignDate = System.DateTime.Now;
            }
            if (this.User.UserId == 67 || this.User.UserId == 144 || User.IsSuperAdmin)
            {
                objQlty.ZLSituationDate = System.DateTime.Now;
            }

            bool r= AgentQltyController.UpdateZLAssign(objQlty);
            if (r)
                Alert.Show("操作成功");
            else
                Alert.Show("操作失败.", "提示", MessageBoxIcon.Error);
        }
    }
}